<template>
    <div class="fjdsaklfdjs">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="realName">
                <el-input v-model="ruleForm.realName"></el-input>
            </el-form-item>
            <el-form-item label="内部账号" prop="userName">
                <el-input v-model="ruleForm.userName"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" prop="password1">
                <el-input type="password" v-model="ruleForm.password1"></el-input>
            </el-form-item>

            <el-form-item label="再次输入" prop="password2">
                <el-input type="password" v-model="ruleForm.password2"></el-input>
            </el-form-item>

            <el-form-item label="所属科室" prop="deptID">
                <el-select
                        v-model="ruleForm.deptID"
                        style="width: 150px;"
                        v-loading="dept_loading">
                    <el-option
                            v-for="(item,index) in Depts"
                            :key="item.id"
                            :label="item.deptName"
                            :value="index">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="工作方案" prop="ruleID">
                <el-select
                        v-model="ruleForm.ruleID"
                        style="width: 150px;"
                        v-loading="rule_loading">
                    <el-option
                            v-for="(item,index) in Rules"
                            :key="item.id"
                            :label="item.ruleName"
                            :value="index">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即入职</el-button>
                <el-button @click="resetForm('ruleForm')">重新输入</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "docReg",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.password1 !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password1) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    realName: '',
                    userName: '',
                    password1: '',
                    password2: '',
                    deptID:0,
                    ruleID:0,
                },
                rules: {
                    realName: [
                        {required: true, message: '请输入真实姓名', trigger: 'blur'},
                        {min: 3, max: 18, message: '长度在 2 到 18 个字符', trigger: 'blur'}
                    ],
                    userName: [
                        {required: true, message: '请输入登录账号', trigger: 'blur'},
                        {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
                    ],
                    password1: [
                        { validator: validatePass, trigger: 'blur',required:true},
                        {min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur'}
                    ],
                    password2: [
                        { validator: validatePass2, trigger: 'blur',required:true }
                    ],
                },
                Depts: [],
                Rules: [],
                dept_loading: true,
                rule_loading: true,
            };
        },
        methods:{
            InitDept() {
                let url = `http://localhost:8082/dept/all`;
                this.$ajax.get(url).then(res => {
                    this.Depts = res.data;
                    this.dept_loading = false;
                });
            },
            InitRule() {
                let url = `http://localhost:8082/rule/all`;
                this.$ajax.get(url).then(res => {
                    this.Rules = res.data;
                    this.rule_loading = false;
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.ruleForm.deptID=this.Depts[this.ruleForm.deptID].id;
                        this.ruleForm.ruleID=this.Rules[this.ruleForm.ruleID].id;
                        let params="?name="+this.ruleForm.realName;
                        params+= "&account="+this.ruleForm.userName;
                        params+= "&pwd="+this.ruleForm.password1;
                        params+="&deptID="+this.ruleForm.deptID;
                        params+= "&ruleID="+this.ruleForm.ruleID;
                        let url = `http://localhost:8082/user/new`+params;
                        // alert(url)
                        this.$ajax.get(url).then(res => {
                            if(res.data=="0"){
                                this.$message({
                                    type: 'success',
                                    message: '入职成功!'
                                });
                                this.resetForm("ruleForm");
                            }else{
                                this.$message({
                                    type: 'info',
                                    message: '请重试'
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted(){
            this.InitDept();
            this.InitRule();
        }
    }
</script>

<style scoped>
    .fjdsaklfdjs {
        width: 60%;
        margin: 0 auto;
    }
</style>